from matplotlib import pyplot as plt
from matplotlib import font_manager
from lxml import etree
import requests
if __name__ == '__main__':
    my_font = font_manager.FontProperties(fname='C:/Windows/Fonts/SIMHEI.TTF')
    url = 'http://www.boxofficecn.com/the-red-box-office'
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/109.0.1518.55'
    }
    page_text = requests.get(url=url,headers=headers).text
    tree = etree.HTML(page_text)
    tr_list = tree.xpath('//*[@id="tablepress-4"]/tbody/tr')
    title_tuple = []
    box_tuple = []
    for tr in tr_list[0:10]:
        title = tr.xpath('./td[2]/text()')[0]
        title_tuple.append(title)
        box = tr.xpath('./td[@class="column-4"]/text() | ./td[4]/font/text()')[0]
        box_tuple.append(box)
        pass
    plt.figure(figsize=(20,8),dpi=80)
    plt.barh(range(len(title_tuple)), box_tuple)
    plt.yticks(range(len(title_tuple)), title_tuple,fontproperties=my_font)
    plt.show()
